<?php

class Products extends CI_Controller {

    public $_updir = 'public/template/backend/images/products/';
    private $_updat;
    private $_auth;

    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if ($this->_auth == FALSE)
            redirect("backend/home");
    }

    public function index() {
        $config['first_link']='Đầu';
        $config['last_link'] = 'Cuối';
        $config['cur_tag_open'] = "<a class='selected'>";
        $config['cur_tag_close'] = "</a>";
        $config['full_tag_open'] = "<div class='paging'>";
        $config['full_tag_close'] = '</div';
        
        $this->load->library('pagination');
        $config['uri_segment'] = 4;
        $config['base_url'] = base_url() . 'backend/products/index';
        $config['total_rows'] = count($this->mproducts->fetchAll());
        $config['per_page'] = 3;
        $this->pagination->initialize($config);
        $data['data']['listpro'] = $this->mproducts->fetchAll($config['per_page'], $this->uri->segment(4));
        $data['data']['paging'] = $this->pagination->create_links();

        $data['title'] = "Bảng sản phẩm";
        $data['temp'] = "backend/products/home";
        $this->load->view("backend/layout/index", $data);
    }

    public function add() {
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $this->form_validation->set_rules('data[name]', 'Tên sản phẩm', 'trim|required|is_unique[products.name]');
            $this->form_validation->set_rules('data[price]', 'Giá sản phẩm', 'trim|required|integer');
            $this->form_validation->set_rules('data[des]', 'Mô tả', 'trim|required');
            $this->form_validation->set_rules('data[size]', 'Kích thước', 'trim|required');
            $this->form_validation->set_rules('data[qty]', 'Số lượng', 'trim|required|integer|max_length[5]');
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->form_validation->run() == TRUE) {
                if ($this->upload->do_upload('img')) {
                    $this->_updat = $this->upload->data();
                    $post['img'] = $this->_updir . $this->_updat['file_name'];
                    $pid = $this->mcategories->getParentId($post['cid']);
                    $post['pid'] = $pid->pid;
                    $this->mproducts->insert($post);
                    redirect("backend/products");
                } else {
                    $data['data'] = array('error' => $this->upload->display_errors());
                }
            }
        }
        $data['data']['parcat'] = $this->mcategories->getParentCat();
        $data['title'] = "Thêm sản phẩm";
        $data['temp'] = "backend/products/form";
        $this->load->view("backend/layout/index", $data);
    }

    public function delete($id) {
        $this->mproducts->delete($id);
        $this->mimages->deleteAll($id);
        $this->msize->deleteAll($id);
        $this->mbonus->deleteAll($id);
        $this->mcolor->deleteAll($id);
        redirect("backend/products");
    }

    public function deleteall() {
        $this->mproducts->deleteAll();
        $this->mimages->emptAll();
        $this->msize->emptAll();
        $this->mbonus->emptAll();
        $this->mcolor->emptAll();
        redirect("backend/products");
    }

    public function update($id) {
        $data['data']['pro']= $this->mproducts->fetchOne($id);
        $data['data']['parcat'] = $this->mcategories->getParentCat();
        if ($this->input->post("add")) {
            $post = $this->input->post("data");
        }
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        $this->form_validation->set_rules('data[name]', 'Tên sản phẩm', 'required');
        $this->form_validation->set_rules('data[price]', 'Giá sản phẩm', 'required');
        $this->form_validation->set_rules('data[des]', 'Mô tả', 'required');
        $this->form_validation->set_rules('data[size]', 'Kích thước', 'required');
        $this->form_validation->set_rules('data[qty]', 'Số lượng', 'trim|required|integer|max_length[5]');
        
        if ($this->form_validation->run() == TRUE){
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('img')) {
                $this->_updat = $this->upload->data();
                $post['img'] = $this->_updir . $this->_updat['file_name'];
            } else {
                $post['img'] = $data['data']['pro']->img;
            }
            
            $this->pid = $this->mcategories->getParentId($post['cid']);
            $post['pid'] = $this->pid->pid;
            $this->mproducts->update($id,$post);
            redirect("backend/products");
        }
        
        $data['title'] = "Sửa sản phẩm";
        $data['temp'] = "backend/products/form";
        $this->load->view("backend/layout/index", $data);
    }

}
